package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import Entities.RegisterClientEntity;
import GUI.CustomerRelationsGUI;
import GUI.RegisterClientGUI;
import Server.RsWraper;
import Server.Tuple;

/**
 * The Class CustomerRelationsController.
 * Controller for customer relations employee activities.
 */
public class CustomerRelationsController implements Observer{
	/**
	 * This is the register boundary.
	 */
	private CustomerRelationsGUI customerRelationsGUI;
	
	/** The tuples. */
	private ArrayList<Tuple>tuples;
	
	/** The delete flag. */
	private int deleteFlag=0;
	
	/** The insert order. */
	private int insertOrder=0;
	
	/** The customers query. */
	private String cQuery;
	
	/** The users query. */
	private String uQuery;
	
	/**
	 * This is the constructor.
	 *
	 * @param customerRelationsGUI the customer relations gui
	 */
	public CustomerRelationsController(CustomerRelationsGUI customerRelationsGUI) {
		this.customerRelationsGUI = customerRelationsGUI;
		ClientController.setObserver(this);
		this.customerRelationsGUI.addRegisterNewClientActionListener(new RegisterNewClientListener() );
		this.customerRelationsGUI.addRegisterClientsActionListener(new RegisterClientsFromListListener());
		this.customerRelationsGUI.setVisible(true);
	}
	
	/**
	 * Gets the customer relations gui.
	 *
	 * @return the customer relations gui
	 */
	public CustomerRelationsGUI getCustomerRelationsGUI() {
		return customerRelationsGUI;
	}

	/**
	* RegisterListener class implements ActionListener interface.
	* this class controls the register button.
	 * @author Ofek-az
	 *
	 */
	class RegisterNewClientListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			RegisterClientGUI registerClientGUI = new RegisterClientGUI();
			new RegisterClientController(registerClientGUI,new RegisterClientEntity()).setPrev(CustomerRelationsController.this);
			LoginController.mainWindow.setContentPane(registerClientGUI);
			
			
		}
		
	}
	
	/**
	 * The listener interface for receiving registerClientsFromList events.
	 * The class that is interested in processing a registerClientsFromList
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addRegisterClientsFromListListener<code> method. When
	 * the registerClientsFromList event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see RegisterClientsFromListEvent
	 */
	class RegisterClientsFromListListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			String validCustomers="SELECT * FROM potentialcustomers WHERE (cid)NOT IN (SELECT uid FROM users)";
			
			try {
				ClientController.observableClient.sendToServer(validCustomers);
			} catch (IOException e1) {e1.printStackTrace();}
		}
		
	}
	
	/* (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		String msg=arg1.toString();
		
		
		if(msg.equals("empty query"))
			customerRelationsGUI.displayWarningMessage("There are no potential customers to add.");
		else if(msg.startsWith("#OC")){System.out.println(msg);}
		else if(!msg.equals("#Server:Action succeded"))
		{
			tuples=((RsWraper)arg1).getRows();
			cQuery="INSERT INTO customers (`cid`) VALUES";
			uQuery="INSERT INTO users (`uid`, `password`, `name`, `livingArea`, `age`, `phoneNumber`, `email`, `loginStatus`) VALUES";
			if(tuples.size()>0){
				cQuery+=" ('"+tuples.get(0).get("CID")+"')";
				uQuery+=" ('"+tuples.get(0).get("CID")+"', '"+tuples.get(0).get("PASSWORD")+"', '"+tuples.get(0).get("NAME")+"', '"+tuples.get(0).get("LIVINGAREA")+"','"+tuples.get(0).get("AGE")+"', '"+tuples.get(0).get("PHONENUMBER")+"', '"+tuples.get(0).get("EMAIL")+"', '0')";
			}
			for(int i=1;i<tuples.size();i++){	//multiple rows inserting
				cQuery+=",('"+tuples.get(i).get("CID")+"')";
				uQuery+=",('"+tuples.get(i).get("CID")+"', '"+tuples.get(i).get("PASSWORD")+"', '"+tuples.get(i).get("NAME")+"', '"+tuples.get(i).get("LIVINGAREA")+"','"+tuples.get(i).get("AGE")+"', '"+tuples.get(i).get("PHONENUMBER")+"', '"+tuples.get(i).get("EMAIL")+"', '0')";
			}
			
			try {
				ClientController.observableClient.sendToServer(cQuery);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			insertOrder=1;
		}
		else if(msg.equals("#Server:Action succeded"))
		{
			
			if(insertOrder==1)
			{
				try {
					ClientController.observableClient.sendToServer(uQuery);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				insertOrder=0;
				deleteFlag=1;
			}
			if(deleteFlag==1)//Inserting completed and we can delete the potential customers list.
			{
				customerRelationsGUI.displayMessage("Customers added successfully");
			
					deleteFlag=0;
			}
		}
		
		
	}

}